package com.sangfor.ssl.common;

import android.content.Context;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import androidx.core.os.EnvironmentCompat;
import com.sangfor.bugreport.logger.Log;
import com.sangfor.ssl.common.IDeviceIdStorage;
import com.sangfor.ssl.service.auth.AuthNativesManager;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
import kotlin.UByte;

/* loaded from: classes2.dex */
public class DeviceIDManger {
    private static final String TAG = "DeviceIDManger";
    private String mDeviceName;
    private String mMacMd5;
    private String mMobileId;
    private DeviceIdStorageManager mStorageManager;

    private native int storeDeviceId(String str, String str2, String str3);

    public String getMacMd5() {
        return this.mMacMd5;
    }

    public String getMobileId() {
        return this.mMobileId;
    }

    public String getDeviceName() {
        return this.mDeviceName;
    }

    public void initDeviceId(Context context) {
        if (context == null) {
            Log.error(TAG, "context is null");
            return;
        }
        DeviceIdStorageManager deviceIdStorageManager = DeviceIdStorageManager.getInstance();
        this.mStorageManager = deviceIdStorageManager;
        if (!deviceIdStorageManager.isInited()) {
            SharedDeviceIdStorage create = SharedDeviceIdStorage.create(context);
            if (create == null) {
                Log.error(TAG, "DeviceIdStorageManager not inited and try create SharedDeviceIdStorage failed");
            } else {
                this.mStorageManager.init(context, new PrivateDeviceIdStorage(context), create);
            }
        }
        this.mMacMd5 = initIMEIMd5(context);
        this.mMobileId = initMobileId(context);
        this.mDeviceName = initDeviceName();
        if (this.mMacMd5 == null) {
            this.mMacMd5 = getMacAddressMd5(context);
        }
        if (this.mMacMd5 == null) {
            this.mMacMd5 = EnvironmentCompat.MEDIA_UNKNOWN;
        }
    }

    public void stroeDeviceId() {
        storeDeviceId(this.mMacMd5, this.mMobileId, this.mDeviceName);
    }

    public static String getAndroidID(Context context) {
        String id = DeviceIdStorageManager.getInstance().getId(IDeviceIdStorage.IdType.ANDROID_ID);
        return (Build.VERSION.SDK_INT < 26 || TextUtils.isEmpty(id)) ? Settings.Secure.getString(context.getContentResolver(), "android_id") : id;
    }

    private String initMobileId(Context context) {
        return getMD5Value(AuthNativesManager.getInstance().nEncryptoMobileId(getAndroidID(context)));
    }

    private String initIMEIMd5(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService("phone");
        String str = "";
        try {
            if (VpnCommon.checkDeviceIdPermission(context)) {
                str = telephonyManager.getDeviceId();
            }
        } catch (Exception e) {
            Log.error(TAG, "get deviceId fail", e.getMessage());
        }
        if (TextUtils.isEmpty(str)) {
            str = Settings.Secure.getString(context.getContentResolver(), "android_id");
            if (TextUtils.isEmpty(str)) {
                return getAndroidID(context);
            }
        }
        return getMD5Value(str);
    }

    private String getMacAddressMd5(Context context) {
        String wifiMac = DeviceInfo.getWifiMac(context);
        if (TextUtils.isEmpty(wifiMac)) {
            return null;
        }
        return getMD5Value(wifiMac);
    }

    private String byte2hex(byte[] bArr) {
        StringBuffer stringBuffer = new StringBuffer(bArr.length);
        for (byte b : bArr) {
            String hexString = Integer.toHexString(b & UByte.MAX_VALUE);
            if (hexString.length() == 1) {
                stringBuffer.append("0");
                stringBuffer.append(hexString);
            } else {
                stringBuffer.append(hexString);
            }
        }
        return String.valueOf(stringBuffer);
    }

    private String initDeviceName() {
        String str;
        try {
            str = Build.MODEL;
        } catch (Exception e) {
            Log.error(TAG, e.getMessage());
            str = null;
        }
        return (str == null || str.length() == 0) ? EnvironmentCompat.MEDIA_UNKNOWN : str;
    }

    public int getRandCode() {
        return new Random(System.currentTimeMillis()).nextInt();
    }

    private String getMD5Value(String str) {
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.update(str.getBytes());
            byte[] digest = messageDigest.digest();
            return "" + byte2hex(digest);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }

    private DeviceIDManger() {
        this.mMacMd5 = null;
        this.mMobileId = null;
        this.mDeviceName = null;
    }

    /* loaded from: classes2.dex */
    private static final class SingletonHolder {
        private static final DeviceIDManger INSTANCE = new DeviceIDManger();

        private SingletonHolder() {
        }
    }

    public static final DeviceIDManger getInstance() {
        return SingletonHolder.INSTANCE;
    }
}
